/*
* $Id: ButtonDraw.cpp 157 2010-09-29 19:15:28Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ButtonDraw.h"
#include "KeyProcess.h"
#include "../../DrawerInterface.h"
#include "../../../UserMessage.h"

CButtonDraw::CButtonDraw(const std::tstring& strName, IDrawer* pDrawer, int nId)
: CDrawControl(strName), m_bPush(false), m_pDrawer(pDrawer), m_nId(nId){
	m_aKeyProcess.push_back(new CSpace(this));
}
CButtonDraw::~CButtonDraw(){}

void CButtonDraw::Resize(){}

void CButtonDraw::PaintActive(CDC* dc){
	PaintNonActive(dc);
	CPoint c_point = m_rect.CenterPoint();
	dc->DrawFocusRect(CRect(m_rect.left + 2, c_point.y - 10, m_rect.right - 6, c_point.y + 9));
}

void CButtonDraw::PaintNonActive(CDC* dc){
	CPoint c_point = m_rect.CenterPoint();
	CRect rectButton(m_rect.left, c_point.y - 12, m_rect.right - 3, c_point.y + 12);
	dc->DrawFrameControl(rectButton, DFC_BUTTON, DFCS_BUTTONPUSH | (m_bPush ? DFCS_PUSHED : 0));
	dc->SetBkMode(TRANSPARENT);
	dc->DrawText(m_strValue.c_str(), rectButton, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	dc->SetBkMode(BKMODE_LAST);
}

bool CButtonDraw::ProcessMouse(unsigned int nChar, CPoint pos, int param){
	CPoint c_point = m_rect.CenterPoint();
	CRect rectButton(m_rect.left, c_point.y - 12, m_rect.right - 3, c_point.y + 12);
	if(m_bPush && rectButton.PtInRect(pos) && nChar == WM_LBUTTONUP){
		m_pDrawer->PostSignal(UM_BUTTONCLICK, m_nId, 0);
		m_bPush = false;
		return true;
	}
	if(	nChar == WM_MOUSEMOVE && !rectButton.PtInRect(pos) && m_bPush || nChar == WM_MOUSELEAVE){
		m_bPush = false;
		return true;
	}else if(rectButton.PtInRect(pos) && nChar == WM_LBUTTONDOWN){
		m_bPush = true;
		return true;
	}
	return false;
}

bool CButtonDraw::ProcessKey(unsigned int nChar, byte stateSysKey){
	if(nChar == VK_SPACE || nChar == VK_RETURN){
		m_pDrawer->PostSignal(UM_BUTTONCLICK, m_nId, 0);
		return true;
	}
	return false;
}
